﻿
Imports System.Web.Configuration
Imports System.Configuration
Imports System.Configuration.Provider
Imports SonOVB.Samples.ComicSyndication.ComicProvider
Imports SonOVB.Samples.ComicSyndication.Configuration
Imports SonOVB.Samples.ComicSyndication.Extensions



Namespace ComicProvider


    ''' <summary>
    ''' 漫画管理器
    ''' </summary>
    ''' <remarks>封装对Provider 的操作</remarks>
    Public NotInheritable Class ComicManager


#Region "初始化"


        ''' <summary>
        '''  已初始化
        ''' </summary>
        ''' <remarks></remarks>
        Private Shared isInitialized As Boolean
        ''' <summary>
        ''' 初始化异常
        ''' </summary>
        ''' <remarks></remarks>
        Private Shared initializationException As Exception

        ''' <summary>
        '''  初始化锁
        ''' </summary>
        ''' <remarks></remarks>
        Private Shared initializationLock As New Object()

        ''' <summary>
        ''' 默认 Prvider
        ''' </summary>
        ''' <remarks></remarks>
        Private Shared defaultProvider As ComicProvider
        ''' <summary>
        '''  获取 默认 Proverider
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared ReadOnly Property Provider() As ComicProvider
            Get
                Initialize()
                Return defaultProvider
            End Get
        End Property

        ''' <summary>
        '''  Proverider 集合
        ''' </summary>
        ''' <remarks></remarks>
        Private Shared providerCollection As ComicProviderCollection
        ''' <summary>
        '''  Proverider 集合
        ''' </summary>
        Public Shared ReadOnly Property Providers() As ComicProviderCollection
            Get
                Initialize()
                Return providerCollection
            End Get
        End Property


        ''' <summary>
        ''' 初始化
        ''' </summary>
        ''' <remarks></remarks>
        Private Shared Sub Initialize()
            If isInitialized Then
                If initializationException IsNot Nothing Then
                    Throw initializationException
                End If
            Else

                If initializationException IsNot Nothing Then
                    Throw initializationException
                End If

                SyncLock initializationLock
                    If isInitialized Then
                        If initializationException IsNot Nothing Then
                            Throw initializationException
                        End If
                    Else

                        Try
                            '获取配置文件 设置
                            Dim qc = ConfigManager.GetSettings

                            If qc.DefaultProvider Is Nothing OrElse String.IsNullOrEmpty(qc.DefaultProvider.Value) _
                              OrElse qc.Providers Is Nothing OrElse qc.Providers.Count < 1 Then
                                Throw New ProviderException("You must specify a valid default provider.")
                            End If

                            '初始化  providers
                            providerCollection = New ComicProviderCollection
                            ProvidersHelper.InstantiateProviders(qc.Providers, providerCollection, GetType(ComicProvider))
                            providerCollection.SetReadOnly()

                            defaultProvider = providerCollection(qc.DefaultProvider.Value)

                            If defaultProvider Is Nothing Then
                                Throw New ConfigurationErrorsException("You must specify a default provider for the feature.", _
                                                                       qc.ElementInformation.Properties(ComicServiceSection.DefaultProviderKey).Source, _
                                                                       qc.ElementInformation.Properties(ComicServiceSection.DefaultProviderKey).LineNumber)
                            End If
                        Catch ex As Exception

                            initializationException = ex
                            Throw
                        End Try

                        isInitialized = True
                    End If
                End SyncLock
            End If
        End Sub




#End Region





        ''' <summary>
        ''' 获取 全部漫画
        ''' </summary>
        Public Shared Function GetComics() As List(Of Comic)
            Return Provider.GetComics
        End Function


        ''' <summary>
        ''' 获取漫画
        ''' </summary>
        ''' <param name="comicId">漫画ID</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetComicById(ByVal comicId As String) As Comic

            If comicId.IsNullOrEmpty Then Throw New ArgumentNullException("comicId")


            Return Provider.GetComicById(comicId)

        End Function

        ''' <summary>
        ''' 获取 全部卷
        ''' </summary>
        ''' <param name="comicId">漫画ID</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetComicVols(ByVal comicId As String) As List(Of ComicContent)
            If comicId.IsNullOrEmpty() Then Throw New ArgumentNullException("comicId")

            Return Provider.GetComicVols(comicId)

        End Function

        ''' <summary>
        ''' 获取 卷
        ''' </summary>
        ''' <param name="comicId">漫画ID</param>
        ''' <param name="volId">卷ID</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetComicVolById(ByVal comicId As String, ByVal volId As String) As ComicContent

            If comicId.IsNullOrEmpty() Then Throw New ArgumentNullException("comicId")
            If volId.IsNullOrEmpty() Then Throw New ArgumentNullException("volId")

            Return Provider.GetComicVolById(comicId, volId)

        End Function


        ''' <summary>
        '''  获取 全部 章
        ''' </summary>
        ''' <param name="comicId">漫画ID</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetComicChapters(ByVal comicId As String) As List(Of ComicContent)

            If comicId.IsNullOrEmpty() Then Throw New ArgumentNullException("comicId")

            Return Provider.GetComicChapters(comicId)

        End Function

        ''' <summary>
        ''' 获取 章
        ''' </summary>
        ''' <param name="comicId">漫画ID</param>
        ''' <param name="chapterId">章ID</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetComicChapterById(ByVal comicId As String, ByVal chapterId As String) As ComicContent

            If comicId.IsNullOrEmpty() Then Throw New ArgumentNullException("comicId")
            If chapterId.IsNullOrEmpty() Then Throw New ArgumentNullException("chapterId")

            Return Provider.GetComicChapterById(comicId, chapterId)

        End Function


        ''' <summary>
        '''  获取图片 从 章  
        ''' </summary>
        ''' <param name="comicId">漫画ID</param>
        ''' <param name="chapterId">章ID</param>
        ''' <param name="pictureId">图片 ID </param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetPictureFromChapter(ByVal comicId As String, ByVal chapterId As String, ByVal pictureId As String) As Picture
            If comicId.IsNullOrEmpty() Then Throw New ArgumentNullException("comicId")
            If chapterId.IsNullOrEmpty() Then Throw New ArgumentNullException("chapterId")
            If pictureId.IsNullOrEmpty() Then Throw New ArgumentNullException("pictureId")

            Return Provider.GetPictureFromChapter(comicId, chapterId, pictureId)

        End Function

        ''' <summary>
        '''  获取图片 从 卷  
        ''' </summary>
        ''' <param name="comicId">漫画ID</param>
        ''' <param name="volId">卷ID</param>
        ''' <param name="pictureId">图片 ID </param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetPictureFromVol(ByVal comicId As String, ByVal volId As String, ByVal pictureId As String) As Picture

            If comicId.IsNullOrEmpty() Then Throw New ArgumentNullException("comicId")
            If volId.IsNullOrEmpty() Then Throw New ArgumentNullException("chapterId")
            If pictureId.IsNullOrEmpty() Then Throw New ArgumentNullException("pictureId")

            Return Provider.GetPictureFromChapter(comicId, volId, pictureId)

        End Function




    End Class



End Namespace
